package com.situ.mall.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.pojo.entity.Product;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.service.IProductService;
import com.situ.mall.common.util.LayUITableResult;

@Service
public class ProductServiceImpl implements IProductService {
	private Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);

	@Autowired
	private ProductMapper productMapper;

	@Override
	public List<Product> selectAll() {
//		ProductExample productExample = new ProductExample();
//		Criteria criteria = productExample.createCriteria();
//		criteria.andNameLike("%Apple%");
//		criteria.andStockGreaterThanOrEqualTo(2000);
//		return productMapper.selectByExample(productExample);
		
		return productMapper.selectAll();
	}

	@Override
	public LayUITableResult selectByPage(Integer page, Integer limit) {
		// 一、记录开始执行时间 1970年1月1号0点0分到现在的毫秒数  millisecond：毫秒
		long begin = System.currentTimeMillis();

		// 1、使用PageHelper设置分页
		PageHelper.startPage(page, limit);
		// int offset = (page - 1) * limit;
		// 2、执行查询
		// List<Product> list = productMapper.selectByPage(offset, limit);
		List<Product> list = productMapper.selectByPage();
		// 3、使用PageInfo对结果进行包装
		PageInfo pageInfo = new PageInfo(list);
		int count = (int)pageInfo.getTotal();
		// int count = productMapper.selectTotalCount();

		/*try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}*/
		// 二、记录结束执行时间
		long end = System.currentTimeMillis();
		long takeTime = end - begin;
		if (takeTime >= 3000) {
			logger.error("===执行结束，花费：{}毫秒===", takeTime);
		} else if (takeTime >= 2000 && takeTime < 3000) {
			logger.warn("===执行结束，花费：{}毫秒===", takeTime);
		} else {
			logger.info("===执行结束，花费：{}毫秒===", takeTime);
		}

		return LayUITableResult.ok(count, list);
	}

	@Override
	public void insert(Product product) {
		productMapper.insert(product);
	}

	@Override
	public List<Product> selectByCategoryId(Integer categoryId) {
		return productMapper.selectByCategoryId(categoryId);
	}

	@Override
	public Product getProductDetailPage(Integer id) {
		return productMapper.selectByPrimaryKey(id);
	}

}
